import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;


public class Telecommande implements ActionListener,WindowListener {

	private JDialog fenetre;
	private JPanel panneau;
	private JPanel pavePrincipal,paveNord,paveCentre,paveSud;
	private JButton haut;
	private JButton gauche;
	private JButton droite;
	private JButton bas;
	private JButton zero,un,deux,trois,quatre,cinq,six,sept,huit,neuf,entree,c,eff,arret;
	private Box paveHaut,paveMilieu,paveBas,paveTouche,dBox,valider,pavePadH,pavePadM,pavePadB;
	private Font police;
	private Logo logo;
	private Editeur edit;
	private BufferedImage capture;
	private Image imageG;
	private ImageIcon left;

    public Telecommande(Logo logo) {
    	
    	this.logo=logo;
    	this.edit=logo.getEdit();
    	fenetre=new JDialog();
    	fenetre.setTitle("Telecommande");
    	panneau=new JPanel();
    	pavePrincipal=new JPanel();
    	paveNord=new JPanel();
    	paveCentre=new JPanel();
    	paveSud=new JPanel();    	    		
    	CreerPanneau();
    	CreerFenetre();
    	fenetre.addWindowListener(this);    	    	   
    }
    
    public void CreerPave()
    {
    	pavePrincipal.setLayout(new BorderLayout());
    	paveNord.setLayout(new BorderLayout());
    	paveCentre.setLayout(new BorderLayout());
    	paveSud.setLayout(new BorderLayout());
    	pavePrincipal.add(paveNord,BorderLayout.NORTH);
    	pavePrincipal.add(paveCentre,BorderLayout.CENTER);   
    	pavePrincipal.add(paveSud,BorderLayout.SOUTH); 	
    		
    	paveHaut=Box.createHorizontalBox();
    	paveMilieu=Box.createHorizontalBox();
    	paveBas=Box.createHorizontalBox();
    	paveTouche=Box.createHorizontalBox();
    	dBox=Box.createHorizontalBox();
    	valider=Box.createHorizontalBox();
    	pavePadH=Box.createHorizontalBox();
    	pavePadM=Box.createHorizontalBox();
    	pavePadB=Box.createHorizontalBox();
    	
    	    	
    	paveNord.add(paveHaut,BorderLayout.NORTH);
    	paveNord.add(paveMilieu,BorderLayout.CENTER);
    	paveNord.add(paveBas,BorderLayout.SOUTH);
    	   	
    	 
    	paveCentre.add(pavePadH,BorderLayout.NORTH);
    	paveCentre.add(pavePadM,BorderLayout.CENTER);
    	paveCentre.add(pavePadB,BorderLayout.SOUTH);
    	
    	paveSud.add(paveTouche,BorderLayout.NORTH);
    	paveSud.add(valider,BorderLayout.CENTER);
    	paveSud.add(dBox,BorderLayout.SOUTH);
    
        	    	
    	zero = new JButton("0");
    	un = new JButton("1");
    	deux = new JButton("2");
    	trois = new JButton("3");
    	quatre = new JButton("4");
    	cinq = new JButton("5");
    	six = new JButton("6");
    	sept = new JButton("7");
    	huit = new JButton("8");
    	neuf = new JButton("9");
    	c = new JButton("C");
    	entree = edit.getBoutonExecuter();
    	entree.addActionListener(this);  
    	eff = new JButton("E");  
    	arret = edit.getBoutonAnnuler();
    	arret.addActionListener(this);
    	 	
    	
    	police = new Font("Courrier new",Font.PLAIN,30);//on definit un style
    
    	
    	zero.addActionListener(this);
    	un.addActionListener(this);
    	deux.addActionListener(this);
    	trois.addActionListener(this);
    	quatre.addActionListener(this); 
    	cinq.addActionListener(this);	
    	six.addActionListener(this);
    	sept.addActionListener(this);
    	huit.addActionListener(this);
    	neuf.addActionListener(this);
    	c.addActionListener(this);
    	eff.addActionListener(this); 
    
    	   	
    	paveHaut.add(un);
    	paveHaut.add(deux);
    	paveHaut.add(trois);
    	paveMilieu.add(quatre);
    	paveMilieu.add(cinq);
    	paveMilieu.add(six);
    	paveBas.add(sept);
    	paveBas.add(huit);
    	paveBas.add(neuf);
    	
    	pavePadH.add(c);    	
    	pavePadH.add(zero);    	
    	pavePadH.add(eff);
    	pavePadM.add(Box.createRigidArea(new Dimension(0,50)));
    	pavePadM.add(Box.createRigidArea(new Dimension(35,0)));	
    	pavePadM.add(haut);
    	
    	pavePadB.add(gauche);
    	pavePadB.add(Box.createRigidArea(new Dimension(15,0)));
    	pavePadB.add(droite);
    	paveTouche.add(Box.createRigidArea(new Dimension(35,0)));
    	paveTouche.add(bas);    	
    	paveTouche.add(Box.createRigidArea(new Dimension(0,50)));
    	
       	    	    	
    	
    	valider.add(Box.createRigidArea(new Dimension(20,0)));	
    	valider.add(entree);
    	
    	dBox.add(Box.createRigidArea(new Dimension(27,0)));
    	dBox.add(arret);
    	
    	   	   	    	    	
    }
        
    public void CreerPanneau()
    {	
    	haut = new JButton();
    	haut.setIcon(new ImageIcon(Logo.IflecheH));
    	haut.addActionListener(this);    	
    	gauche = new JButton();
    	gauche.setIcon(new ImageIcon(Logo.IflecheG));
    	gauche.addActionListener(this);
    	droite = new JButton();
    	droite.setIcon(new ImageIcon(Logo.IflecheD));
    	droite.addActionListener(this);
    	bas = new JButton();
    	bas.setIcon(new ImageIcon(Logo.IflecheB)); 
    	bas.addActionListener(this);   	
    	panneau.setLayout(new BorderLayout());
    	CreerPave();
    	panneau.add(pavePrincipal,BorderLayout.CENTER);
    }
    
    public void CreerFenetre()    {
    	
    	fenetre.getContentPane().add(panneau);
    	fenetre.setVisible(true);
    	fenetre.setResizable(false);
    	fenetre.setLocation(400, 100);
    	fenetre.pack();
    }
    
	public void fermerTelecommande() {
		fenetre.setVisible(false);
	}
    
    public void actionPerformed(ActionEvent e)
    {
    	if(e.getSource()==zero)	edit.getJTexteArea().append("0");     		    	  	    	
    	if(e.getSource()==un) edit.getJTexteArea().append("1");        		
    	if(e.getSource()==deux)  edit.getJTexteArea().append("2");        		
    	if(e.getSource()==trois) edit.getJTexteArea().append("3");    	    	
    	if(e.getSource()==quatre) edit.getJTexteArea().append("4");    	
    	if(e.getSource()==cinq) edit.getJTexteArea().append("5");    	    		
    	if(e.getSource()==six) edit.getJTexteArea().append("6");    	    		
    	if(e.getSource()==sept) edit.getJTexteArea().append("7");    	    		
    	if(e.getSource()==huit) edit.getJTexteArea().append("8");    	    		
    	if(e.getSource()==neuf) edit.getJTexteArea().append("9");    	    		
    	if(e.getSource()==c)
    	{
    		String tmp="";
    		for(int i=0;i<edit.getJTexteArea().getText().length()-1;i++)
    			tmp+=edit.getJTexteArea().getText().substring(i,i+1);
    		    		
    		edit.getJTexteArea().setText(tmp);	
    		
    	}    	
    	if(e.getSource()==eff) edit.getJTexteArea().setText("");    		
    	if(e.getSource()==haut) edit.getJTexteArea().append("\nav ");    		
    	if(e.getSource()==bas) edit.getJTexteArea().append("\nre ");	    			
    	if(e.getSource()==gauche) edit.getJTexteArea().append("\nga ");	    			
    	if(e.getSource()==droite) edit.getJTexteArea().append("\ndr ");	    			    
    }
    
    public void windowClosing(WindowEvent arg0) {
		logo.setTelecommande();
	}
	public void windowClosed(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}	
	public void windowIconified(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowActivated(WindowEvent arg0) {}
	public void windowDeactivated(WindowEvent arg0) {}
       
}